class Foro < ActiveRecord::Base
  has_many :mis_foros
  has_many :users, :through => :mis_foros

  belongs_to :sitio
  has_many :temas

  before_save :actualizar_sumatorio
  #~ before_update :actualizar_sumatorio

  def actualizar_sumatorio
    sumatorio = 0
    self.temas.each { |tema| sumatorio += tema.sumatory }
    #~ puts "actualizar_sumatorio #{self.direccion} -> #{sumatorio}"
    #~ write_attribute('sumatory', sumatorio)
    self.sumatory = sumatorio
    true
  end

  def self.not_subscribed_by_user(user)
    Foro.find(:all).reject { |foro| foro.users.include? user }
  end
end
